package com.guch.help.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.guch.help.activity.ActiveActivity;
import com.guch.help.activity.HistoryClearingRecordActivity;
import com.guch.help.R;
import com.guch.help.api.ApiService;
import com.guch.help.bean.WallPaperResponse;
import com.llw.network.NetworkApi;
import com.llw.network.observer.BaseObserver;
import com.llw.network.utils.KLog;

public class UserFragment extends Fragment {

    private View mView;
    private TextView tvAppActive, tvHistory;

    @Nullable
    @Override
    public View onCreateView(@Nullable LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
        mView = inflater.inflate(R.layout.tab_me_fragment,container,false);
        init();
        return mView;
    }

    //判断当前的fragment显示状态
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        System.out.println("----GST-------hidden-----: " + hidden);
    }


    @Override
    public void onResume() {
        super.onResume();
        System.out.println("----GST-------onResume-----: ");
    }

    private void init() {
        tvAppActive = mView.findViewById(R.id.tv_app_active);
        tvHistory = mView.findViewById(R.id.tv_history);
        tvAppActive.setOnClickListener(v -> gotoActiveActivity(ActiveActivity.class));

        tvHistory.setOnClickListener(v -> gotoActiveActivity(HistoryClearingRecordActivity.class));
    }

    private void gotoActiveActivity(Class<?> classes) {
//        startActivity(new Intent(getActivity(), classes));

        // TODO
//        requestNetwork();

        getWeather();
    }

    /**
     * 访问网络
     */
    @SuppressLint("CheckResult")
    private void requestNetwork() {
        NetworkApi.createService(ApiService.class)
                .getWallPaper()
                .compose(NetworkApi.applySchedulers(new BaseObserver<WallPaperResponse>() {
                    @Override
                    public void onSuccess(WallPaperResponse wallPaperResponse) {
                        KLog.e("MainActivity", "----result---->>>" + wallPaperResponse.toString());
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        KLog.e("MainActivity", e.toString());
                    }
                }));
    }

    private void getWeather() {
        NetworkApi.createService(ApiService.class)
                .searchPlaces("上海市")
                .compose(NetworkApi.applySchedulers(new BaseObserver<String>() {
                    @Override
                    public void onSuccess(String wallPaperResponse) {
                        KLog.e("MainActivity", "----result---->>>" + wallPaperResponse.toString());
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        KLog.e("MainActivity", e.toString());
                    }
                }));
    }

}